﻿namespace SERFLITEMAX20.Core
{
    using SERFLITEMAX20;
    using System;
    using System.Xml;

    public class PlayerObject : UnitBase
    {
        public PlayerObject(int ptr, Main sender) : base(ptr, sender)
        {
            this.Update();
        }

        public override XmlElement GetXMLElement(XmlDocument pDom, string pName)
        {
            XmlElement xMLElement = base.GetXMLElement(pDom, pName);
            xMLElement.SetAttribute("name", this.Name);
            xMLElement.SetAttribute("level", this.Level.GetString());
            xMLElement.SetAttribute("x", base.x.GetString());
            xMLElement.SetAttribute("y", base.y.GetString());
            xMLElement.SetAttribute("z", base.z.GetString());
            xMLElement.SetAttribute("facing", base.facing.GetString());
            xMLElement.SetAttribute("flags", this.Flags.GetString());
            xMLElement.SetAttribute("isdead", this.IsDead.GetString());
            xMLElement.SetAttribute("reaction", this.UnitReactionToPlayer.GetString());
            return xMLElement;
        }

        public override bool Update()
        {
            bool flag = base.Update();
            if (flag)
            {
                base.speed = base.sender.Memory.ReadFloat(base.ptrBase + Addresses.OFFSET_UNIT_Speed);
                if (Globals.player == this)
                {
                    base.x = base.sender.Memory.ReadFloat(Addresses.STATIC_PLAYER_Y);
                    base.y = base.sender.Memory.ReadFloat(Addresses.STATIC_PLAYER_X);
                    base.z = base.sender.Memory.ReadFloat(Addresses.STATIC_PLAYER_Z);
                    base.facing = base.sender.Memory.ReadFloat(Addresses.STATIC_PLAYER_Z + 40);
                    return flag;
                }
                base.x = base.sender.Memory.ReadFloat(base.ptrBase + Addresses.OFFSET_UNIT_PosX);
                base.y = base.sender.Memory.ReadFloat(base.ptrBase + Addresses.OFFSET_UNIT_PosY);
                base.z = base.sender.Memory.ReadFloat(base.ptrBase + Addresses.OFFSET_UNIT_PosZ);
                base.facing = base.sender.Memory.ReadFloat(base.ptrBase + Addresses.OFFSET_UNIT_Rotation);
            }
            return flag;
        }

        public int ChannelSpell
        {
            get
            {
                return base.ReadStorageInt(UpdateFields.CONTAINER_FIELD_SLOT_1_13);
            }
        }

        public override string Name
        {
            get
            {
                base.name = (string) Globals.playerNames[base.Guid];
                if (base.name != null)
                {
                    return base.name;
                }
                return "";
            }
        }
    }
}

